package com.gdzn.signal.mongo.service.impl;

import com.gdzn.common.mongo.filter.MongoQueryBuilder;
import com.gdzn.signal.mongo.service.MgSignalAttributeService;
import com.gdzn.signal.mongo.document.MgSignalAttribute;
import com.gdzn.signal.mongo.repository.MgSignalAttributeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class MgSignalAttributeServiceImpl implements MgSignalAttributeService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MgSignalAttributeRepository mgSignalAttributeRepository;

    ExampleMatcher matcher = ExampleMatcher.matching()
//           .withMatcher("stationName",ExampleMatcher.GenericPropertyMatchers.contains())
            .withIgnorePaths("_class").withIncludeNullValues();
//            .withMatcher("devId", ExampleMatcher.GenericPropertyMatchers.contains());

    @Override
    public MgSignalAttribute insert(MgSignalAttribute t) {
        return mgSignalAttributeRepository.save(t);
    }

    @Override
    public List<MgSignalAttribute> insertAll(List<MgSignalAttribute> t) {
        return mgSignalAttributeRepository.saveAll(t);
    }

    @Override
    public void deleteById(String id) {
        mgSignalAttributeRepository.deleteById(id);
    }

    @Override
    public void deleteByIds(String[] ids) {
        mgSignalAttributeRepository.deleteAllById(Arrays.asList(ids));
    }

    @Override
    public MgSignalAttribute update(MgSignalAttribute station) {
        return mongoTemplate.save(station);
    }

    @Override
    public void updateById(String id, Map<String, Object> updateMap) {
        // 查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        // 更新条件
        Update update = MongoQueryBuilder.buildUpdate(updateMap);
        mongoTemplate.updateFirst(query, update, MgSignalAttribute.class);

    }

    @Override
    public void updateByParam(Map<String, Object> queryMap, Map<String, Object> updateMap) {
        Criteria criteria = MongoQueryBuilder.buildCriteria(queryMap);
        Query query = Query.query(criteria);
        Update update = MongoQueryBuilder.buildUpdate(updateMap);
        mongoTemplate.updateMulti(query, update, MgSignalAttribute.class);

    }

    @Override
    public List<MgSignalAttribute> findAll() {
        return mgSignalAttributeRepository.findAll();
    }

    @Override
    public List<MgSignalAttribute> findAll(MgSignalAttribute vo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "txnNo");
        return findAll(vo, sort);
    }

    @Override
    public List<MgSignalAttribute> findAll(MgSignalAttribute vo, Sort sort) {
        Example<MgSignalAttribute> example = Example.of(vo, matcher);
        return mgSignalAttributeRepository.findAll(example, sort);
    }

    @Override
    public MgSignalAttribute findById(String id) {
        return mgSignalAttributeRepository.findById(id).orElse(null);
    }

    @Override
    public MgSignalAttribute findOne(MgSignalAttribute vo) {
        Example<MgSignalAttribute> example = Example.of(vo, matcher);
        return mgSignalAttributeRepository.findOne(example).orElse(null);
    }

    @Override
    public Page<MgSignalAttribute> findListPage(MgSignalAttribute vo, int page, int size) {
        Example<MgSignalAttribute> example = Example.of(vo, matcher);
        // 分页参数
        if (page <= 0) {
            page = 1;
        }
        page = page - 1;
        if (size <= 0) {
            size = 10;
        }
        Pageable pageable = PageRequest.of(page, size);
        Page<MgSignalAttribute> result = mgSignalAttributeRepository.findAll(example, pageable);
        return result;
    }

    @Override
    public MgSignalAttribute getLatestAttribute(MgSignalAttribute attribute) {
        Query query = new Query().with(Sort.by(Sort.Direction.DESC, "txnNo")).limit(1);
        return mongoTemplate.findOne(query,MgSignalAttribute.class);
    }
}
